using Microsoft.EntityFrameworkCore;
using Nantianmen;
using Nantianmen.EntityFrameworkCore;

var builder = WebApplication.CreateBuilder(args);

builder.Services.AddProblemDetails();
builder.Services.AddControllers().AddJsonOptions(options =>
{
    options.JsonSerializerOptions.PropertyNamingPolicy = System.Text.Json.JsonNamingPolicy.CamelCase;
});
builder.Services.AddDbContext<NantianmenDbContext>(options =>
    options.UseNpgsql(builder.Configuration.GetConnectionString("Default")));
builder.Services.AddEndpointsApiExplorer();
builder.Services.AddNantianmen();
builder.Services.UseEntityFrameworkCore<NantianmenDbContext>();
builder.Services.AddOpenApiDocument(options =>
{
    options.Title = "Nantianmen APIs";
});
builder.Services.AddCors(options =>
{
    if (builder.Environment.IsDevelopment())
    {
        options.AddDefaultPolicy(policy =>
        {
            policy.AllowAnyOrigin().AllowAnyMethod().AllowAnyHeader();
        });
    }
});

var app = builder.Build();

var apiGroup = app.MapGroup("/api").WithOpenApi();

app.UseNantianmenEndpoints(apiGroup);

app.UseCors();

if (app.Environment.IsDevelopment())
{
    app.UseOpenApi();
    app.UseSwaggerUi();
}

app.Run();
